using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace SiliverlightJavascriptIntelli
{
    public class SilverlightStaticField : IJavascriptConvertable
    {
        private SilverlightType belongTo;
        private string _name;
        private bool _isEnum;
        private bool _isDependencyProperty;

        public bool IsDependencyProperty
        {
            get
            {
                return _isDependencyProperty;
            }
            set
            {
                _isDependencyProperty = value;
            }
        }

        public bool IsEnum
        {
            get
            {
                return _isEnum;
            }
            set
            {
                _isEnum = value;
            }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public SilverlightStaticField(SilverlightType belongTo)
        {
            this.belongTo = belongTo;
        }

        public SilverlightType BelongTo
        {
            get { return belongTo; }
            set { belongTo = value; }
        }

        public static SilverlightStaticField ConvertFieldInfoToSilverlightStaticDependencyProperty(FieldInfo curParameterInfoToConvertToSilverlightStaticField, SilverlightType type)
        {
            SilverlightStaticField newParam = new SilverlightStaticField(type);

            newParam.Name = curParameterInfoToConvertToSilverlightStaticField.Name.Replace("Property", "");
            newParam.IsEnum = TypeHelper.IsEnum(curParameterInfoToConvertToSilverlightStaticField.FieldType);
            newParam.IsDependencyProperty = TypeHelper.IsFromDependencyProperty(curParameterInfoToConvertToSilverlightStaticField.FieldType);

            return newParam;
        }

        public string ToJavascript()
        {
            if (IsEnum)
                return StringHelper.Format(JavascriptFormats.StaticEnumValue, this.Name, this.BelongTo.Name,
                    string.Empty);
            else if (IsDependencyProperty)
                return StringHelper.Format(JavascriptFormats.StaticDependencyProperty, this.Name, this.BelongTo.Name,
                    "DependencyProperty");

            throw new ApplicationException();
        }

        public override bool Equals(object obj)
        {
            if (obj is SilverlightStaticField)
                return this.Name == ((SilverlightStaticField)obj).Name;
            else
                return base.Equals(obj);
        }

    }
}
